# 多阶段构建 - 编译阶段
FROM maven:3.8.4-openjdk-17 AS builder

# 设置工作目录
WORKDIR /build

# 复制shared-components
COPY shared-components ./shared-components

# 先构建shared-components
RUN cd shared-components && mvn clean install -DskipTests -B

# 复制当前服务的pom.xml和源代码
COPY course-management-service/pom.xml ./
COPY course-management-service/src ./src

# 编译当前服务
RUN mvn clean package -DskipTests -B

# 运行阶段
FROM openjdk:17-jdk-slim

# 设置工作目录
WORKDIR /app

# 从构建阶段复制JAR文件
COPY --from=builder /build/target/*.jar app.jar

# 暴露端口
EXPOSE 8083

# 设置JVM参数
ENV JAVA_OPTS="-Xmx512m -Xms256m"

# 启动应用
CMD ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  CMD curl -f http://localhost:8083/actuator/health || exit 1